from celery_tasks.main import celery_app
from .yuntongxun.sms import CCP
import logging
logger = logging.getLogger("django")

# 发送短信验证码模板
SMS_CODE_TEMPLATE = 1


# 发起任务：发送短信验证码
@celery_app.task(name='send_sms_code')
def send_sms_code(mobile, sms_code, expire):
    """

    :param mobile:  手机号
    :param sms_code: 验证码
    :param expire: 有效期
    :return:
    """

    # ccp = CCP()
    # ccp.send_template_sms(mobile, [sms_code, expire], SMS_CODE_TEMPLATE)

    try:
        ccp = CCP()
        result = ccp.send_template_sms(mobile, [sms_code, expire], SMS_CODE_TEMPLATE)
    except Exception as e:
        logger.error("发送验证码短信[异常][ mobile: %s, message: %s ]" % (mobile, e))
    else:
        if result == 0:
            logger.info("发送验证码短信[正常][ mobile: %s ]" % mobile)
        else:
            logger.warning("发送验证码短信[失败][ mobile: %s ]" % mobile)

# 启动celery任务
# 进入celery_tasks所在目录，终端命令：celery -A celery_tasks.main worker -l info
